package top.lvkun.quartz.service.impl;

import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;
import top.lvkun.quartz.domain.QuartzJobInfo;
import top.lvkun.quartz.service.IQuartzService;
import top.lvkun.quartz.utils.QuartzUtils;

/**
 * TODO
 *
 * @author Administrator
 * @version 1.0
 * @date 2021/1/24 14:13
 */
@Service
public class QuartzServiceImpl implements IQuartzService {

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;

    @Override
    public void add(QuartzJobInfo quartzJobInfo) {
        //通过FactoryBean获取Scheduler调度器
        Scheduler scheduler = schedulerFactoryBean.getScheduler();
        //调用工具类添加定时任务
        QuartzUtils.addJob(scheduler,quartzJobInfo.getJobName(),
                quartzJobInfo.getClz(),quartzJobInfo.getParams(),quartzJobInfo.getCronExpression());
    }

    @Override
    public void remove(String jobName) {
        //通过FactoryBean获取Scheduler调度器
        Scheduler scheduler = schedulerFactoryBean.getScheduler();
        //调用工具类删除定时任务
        QuartzUtils.removeJob(scheduler, jobName);
    }
}
